home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / chrome / browser.jar / content / browser / places / demos / time.js < prev    next >
Encoding:
Text File  |  2006-04-26  |  9.2 KB  |  298 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Places
  15.  *
  16.  * The Initial Developer of the Original Code is Google Inc.
  17.  * Portions created by the Initial Developer are Copyright (C) 2006
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Brett Wilson <brettw@gmail.com> (original author)
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. window.addEventListener("load", BW_startup, false);
  38.  
  39. var BW_frame;
  40. var BW_historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"].
  41.            getService(Components.interfaces.nsINavHistoryService);
  42. var BW_result;
  43.  
  44. var loadedIframe = false;
  45. var loadedBretts = false;
  46.  
  47. function BW_startup() {
  48.   BW_frame = document.getElementById("theframe");
  49.  
  50.   var options = BW_historyService.getNewQueryOptions();
  51.   options.sortingMode = options.SORT_BY_DATE_DESCENDING;
  52.   options.resultType = options.RESULTS_AS_VISIT;
  53.   options.maxResults = 200;
  54.   var query = BW_historyService.getNewQuery();
  55.   BW_result = BW_historyService.executeQuery(query, options);
  56.   BW_result.root.containerOpen = true;
  57.  
  58.   BW_frame.contentWindow.addEventListener("load", BW_fill, true);
  59.   loadedBretts = true;
  60.   if (loadedIframe && loadedBretts)
  61.     BW_fill();
  62.   //BW_frame.onload = BW_fill;
  63.   //BW_frame.contentDocument.onLoad = BW_fill;
  64.   //BW_fill();
  65. }
  66.  
  67. function BW_loadiframe() {
  68.   loadedIframe = true;
  69.   if (loadedIframe && loadedBretts)
  70.     BW_fill();
  71. }
  72.  
  73.  
  74. function BW_getTLD(host) {
  75.   var count = 0;
  76.   for (var i = host.length - 2; i > 0; i --) {
  77.     if (host[i] == '.') {
  78.       count ++;
  79.       if (count == 2) {
  80.         return host.substr(i + 1);
  81.       }
  82.     }
  83.   }
  84.   return host;
  85. }
  86.  
  87. var BW_filled = false;
  88.  
  89. function BW_fill() {
  90.   if (BW_filled)
  91.     return;
  92.   BW_filled = true;
  93.  
  94.   BW_frame.setAttribute('onload', '');
  95.  
  96.   var container = BW_result.root;
  97.   var length = container.childCount;
  98.   dump("doc = " + BW_frame.contentDocument + "\n");
  99.   var doc = BW_frame.contentDocument;
  100.  
  101.   var ios = Components.classes["@mozilla.org/network/io-service;1"].
  102.         getService(Components.interfaces.nsIIOService);
  103.   var dateformat = Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
  104.                    .getService(Components.interfaces.nsIScriptableDateFormat);
  105.  
  106.   var table = doc.createElement('table');
  107.   doc.body.appendChild(table);
  108.  
  109.   var counts = new Array(240);
  110.   for (var i = 0; i < counts.length; i ++) {
  111.     counts[i] = 0;
  112.   }
  113.  
  114.   var now = new Date();
  115.   now.setHours(0);
  116.   now.setMinutes(0);
  117.   now.setSeconds(0);
  118.   now.setMilliseconds(0);
  119.   now.setDate(now.getDate()+1);
  120.   var tonightUS = now.getTime() * 1000;
  121.   var usPerHour = 3600000000;
  122.  
  123.   var previousSession = -1;
  124.   var previousMS = 18437736874454810627;
  125.   var previousHost = "";
  126.   for (var i = 0; i < length; i ++) {
  127.     var child = container.getChild(i);
  128.     child.QueryInterface(Components.interfaces.nsINavHistoryVisitResultNode);
  129.     var session = child.sessionId;
  130.  
  131.     var thisBin = Math.floor((tonightUS - child.time) / usPerHour);
  132.     if (thisBin >= 0 && thisBin < counts.length) {
  133.       counts[thisBin] = counts[thisBin] + 1;
  134.     }
  135.  
  136.     var ms = child.time / 1000;
  137.     var addedTime = false;
  138.     if (previousMS - ms > 600000) {
  139.       addedTime = true;
  140.       var t = new Date(ms);
  141.       var tr = doc.createElement('tr');
  142.       table.appendChild(tr);
  143.       var td = doc.createElement('td');
  144.       td.setAttribute('colspan', '2');
  145.       td.setAttribute('class', 'time');
  146.       tr.appendChild(td);
  147.  
  148.       var timestring = dateformat.FormatDateTime("",
  149.                                 dateformat.dateFormatShort,
  150.                                 dateformat.timeFormatNoSeconds,
  151.                                 t.getFullYear(),
  152.                                 t.getMonth(),
  153.                                 t.getDate(),
  154.                                 t.getHours(),
  155.                                 t.getMinutes(),
  156.                                 0);
  157.       var timetext = doc.createTextNode(timestring);
  158.       td.appendChild(timetext);
  159.     }
  160.     previousMS = ms;
  161.  
  162.     var tr = doc.createElement('tr');
  163.     table.appendChild(tr);
  164.  
  165.     // URL
  166.     var spec;
  167.     var uri;
  168.     try {
  169.       spec = child.uri;
  170.       uri = ios.newURI(spec, null, null);
  171.     } catch(e) {
  172.       spec = null;
  173.       uri = null;
  174.     }
  175.  
  176.     // host name if needed on left
  177.     var td = doc.createElement('td');
  178.     td.setAttribute('valign', 'top');
  179.     td.setAttribute('align', 'right');
  180.     td.setAttribute('class', 'host');
  181.     tr.appendChild(td);
  182.     var host = BW_getTLD(uri.host);
  183.     if (addedTime || host != previousHost) {
  184.       // add host name
  185.       var hosttext = doc.createTextNode(host);
  186.       td.appendChild(hosttext);
  187.     }
  188.     previousHost = host;
  189.  
  190.     // right section
  191.     var td = doc.createElement('td');
  192.     td.setAttribute('valign', 'top');
  193.     tr.appendChild(td);
  194.  
  195.     if (! addedTime && (i == 0 || child.sessionId != previousSession))
  196.       td.setAttribute('class', 'itemnew');
  197.     else
  198.       td.setAttribute('class', 'item');
  199.     previousSession = session;
  200.  
  201.     // title div and text
  202.     var titlediv = doc.createElement('div');
  203.     titlediv.setAttribute('class', 'title');
  204.  
  205.     var imgelt = doc.createElement('img');
  206.     if (child.icon)
  207.       imgelt.setAttribute('src', child.icon.spec);
  208.     else
  209.       imgelt.setAttribute('src', 'chrome://browser/skin/places/defaultFavicon.png');
  210.     imgelt.setAttribute('width', 16);
  211.     imgelt.setAttribute('height', 16);
  212.     imgelt.setAttribute('class', 'favicon');
  213.     titlediv.appendChild(imgelt);
  214.  
  215.     var titletext = doc.createTextNode(child.title);
  216.     titlediv.appendChild(titletext);
  217.     td.appendChild(titlediv);
  218.  
  219.     // URL
  220.     if (spec) {
  221.       /* // this does bold host names
  222.       var host = uri.host;
  223.       var hostStart = spec.indexOf(host);
  224.       if (hostStart >= 0) {
  225.         var prehost = spec.substring(0, hostStart);
  226.         var prehosttext = doc.createTextNode(prehost);
  227.         var posthost = spec.substring(hostStart + host.length, spec.length);
  228.         var posthosttext = doc.createTextNode(posthost);
  229.         var hosttext = doc.createTextNode(host);
  230.  
  231.         var boldElement = doc.createElement('b');
  232.         boldElement.appendChild(hosttext);
  233.  
  234.         var urldiv = doc.createElement('div');
  235.         urldiv.setAttribute('class', 'url');
  236.         urldiv.appendChild(prehosttext);
  237.         urldiv.appendChild(boldElement);
  238.         urldiv.appendChild(posthosttext);
  239.         td.appendChild(urldiv);
  240.       }
  241.       */
  242.       var urldiv = doc.createElement('div');
  243.       urldiv.setAttribute('class', 'url');
  244.       var urltext = doc.createTextNode(spec);
  245.       urldiv.appendChild(urltext);
  246.       td.appendChild(urldiv);
  247.     }
  248.   }
  249.  
  250.   // smooth the counts
  251.   var counts2 = new Array(counts.length);
  252.   for (var i = 0; i < counts.length; i ++) {
  253.     var ttl = 0;
  254.     var acc = 0;
  255.     for (var j = -2; j <= 2; j ++) {
  256.       if (i + j < 0) continue;
  257.       if (i + j >= counts.length) continue;
  258.       var scale;
  259.       if (j == -2 || j == 2) scale = 0.33;
  260.       else if (j == -1 || j == 1) scale = 0.66;
  261.       else scale = 1.0;
  262.       acc += counts[i+j] * scale;
  263.       ttl += scale;
  264.     }
  265.     counts2[i] = Math.round(acc);
  266.   }
  267.  
  268.   // fill in the day bargraphs
  269.   var daylist = document.getElementById("daylist");
  270.   for (var i = 0; i < counts2.length / 24; i ++) {
  271.     var day = document.createElement('hbox');
  272.     day.setAttribute('align', 'center');
  273.     if (i % 2)
  274.       day.setAttribute('class', 'day2');
  275.     else
  276.       day.setAttribute('class', 'day1');
  277.     daylist.appendChild(day);
  278.  
  279.     var text = document.createTextNode("Today - " + i );
  280.     var description = document.createElement('description');
  281.     description.setAttribute('flex', '1');
  282.     description.appendChild(text);
  283.     day.appendChild(description);
  284.  
  285.     var bars = document.createElement('vbox');
  286.     bars.setAttribute('align', 'end');
  287.     day.appendChild(bars);
  288.  
  289.     for (var b = 0; b < 24;  b++) {
  290.       var box = document.createElement('hbox');
  291.       box.setAttribute('width', '' + counts2[i*24 + b]);
  292.       box.setAttribute('height', '1');
  293.       box.setAttribute('class', 'green');
  294.       bars.appendChild(box);
  295.     }
  296.   }
  297. }
  298.